package com.ryskoo.catering.productattr.service;

import com.ryskoo.catering.productattr.domain.ProductAttrPO;
import com.ryskoo.catering.productattr.repository.IProductAttrRepository;
import com.ryskoo.catering.productattrvalue.domain.ProductAttrValuePO;
import com.ryskoo.catering.productattrvalue.repository.IProductAttrValueRepository;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
@Transactional
public class ProductAttrServiceImpl extends BasicServiceImpl<ProductAttrPO> implements IProductAttrService{

    @Autowired
    private IProductAttrRepository productAttrRepository;
    @Autowired
    private IProductAttrValueRepository productAttrValueRepository;

    @PostConstruct
    public void init(){
        this.setDao(productAttrRepository);
    }

    @Override
    public void deleteByMainUuid(String mainUUid) {
        /* 1.根据商品uuid查询属性uuids */
        List<String> uuids = productAttrRepository.getUuidsByMainUuid(mainUUid);
        if(!uuids.isEmpty()){
            /* 2.根据属性uuids删除属性值*/
            productAttrValueRepository.deleteByAttrIds(uuids);
            /* 3.删除属性*/
            productAttrRepository.deletes(uuids);
        }
    }

    @Override
    public List<ProductAttrPO> getAttrsByMainUuid(String mainUuid) {
        List<ProductAttrPO> list = productAttrRepository.getAttrsByMainUuid(mainUuid);
        for (ProductAttrPO attr:list) {
            List<ProductAttrValuePO> values = productAttrValueRepository.getValuesByAttrId(attr.getUuid());
            attr.setProductAttrValueList(values);
        }
        return list;
    }

    @Override
    public List<String> getUuidsByMainUuids(List<String> mainUuids) {
        return productAttrRepository.getUuidsByMainUuids(mainUuids);
    }
}
